home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / s342q08.lha / getenv.c < prev    next >
C/C++ Source or Header  |  1994-09-21  |  2KB  |  90 lines

  1. /**
  2.   GETENV - replacement for the standard library version to
  3.            limit the I/O for regular processing of ENV:TZ
  4.            in some library calls.
  5.   Version 0.00 -- initial coding
  6.   Verison 1.00 -- fixed bug in loop, everything operational!
  7. **/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #undef getenv
  14. char *  __getenv(const char *name);
  15.  
  16. struct namelist
  17.   {
  18.   struct namelist *next;  /* link to next item */
  19.   char *name;             /* name of item      */
  20.   char *value;            /* value of item     */
  21.   };
  22.  
  23. static struct namelist *head=NULL;
  24.  
  25. char *  __getenv(const char *name)
  26.   {
  27.   struct namelist *ptr;  /* current item */
  28.   char *value;           /* buffer */
  29.   char filename[128];    /* actual name of ENV variable in file system */
  30.   FILE *ep;              /* file pointer */
  31. /**
  32.   Name is looked up in name list, if not found, an entry is
  33.   created and it will be set to the value of the file ENV:name
  34.   if ENV:name does not exist, the value will be NULL
  35.  
  36.   LIMITATION:  Maximum ENV valiable is BUFSIZ.
  37. **/
  38.   ptr = head;
  39.   while( NULL != ptr )
  40.     {
  41.     if( NULL == strcmp(name,ptr->name) ) break;
  42.     ptr = ptr->next;
  43.     };
  44. /**
  45.   Now we either have the name in our list or it is new.
  46.   If in the list, return a duplicate of the entry value, else
  47.   add it to the list.
  48. **/
  49.  if( NULL == ptr )
  50.    {
  51. /**
  52.   New entry, first we attempt to open the file ENV:name.
  53.   If the file does not exist, Set value to NULL; otherwise
  54.   open the file and read contents into the buffer.
  55. **/
  56.    ptr = (struct namelist *)calloc(1,sizeof(struct namelist));
  57.    if( NULL == ptr )
  58.      {
  59.      return (char *)NULL;          /* no memory available, return NULL */
  60.      };
  61.    value = (char *)calloc(1,BUFSIZ);
  62.    if( NULL == value )
  63.      {
  64.      return (char *)NULL;
  65.      };
  66.    /**
  67.      build the structure, add it to the list
  68.      Now open the file and read the contents
  69.    **/
  70.    ptr->name = strdup(name);
  71.    ptr->value= NULL;
  72.    ptr->next = head;
  73.    head      = ptr;        /* list is most recently used order */
  74.    strcpy(filename, "ENV:");
  75.    strcat(filename,name);
  76.    if( NULL != (ep = fopen(filename,"r")) )
  77.      {
  78.      (void) fread(value, 1, BUFSIZ,ep);
  79.      ptr->value = value;
  80.      fclose(ep);
  81.      }
  82.    else
  83.      {
  84.      free(value);
  85.      };
  86.    };
  87.   return ( ptr->value );
  88.  /*  return ( ptr->value == NULL) ? (char *)NULL : strdup(ptr->value); */
  89.   }
  90.